package com.example.xinyun.provider;

import cn.droidlover.xrecyclerview.XRecyclerView;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.TbFourm;
import com.example.xinyun.db.db_User;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.List;

public class TbFourmProvider<T> extends XRecyclerView.RecyclerAdapter<TbFourm> {

    private List<TbFourm> settingList;
    private Context context;
    private boolean isFTR = false;

    public TbFourmProvider(Context context, List<TbFourm> list) {
        super(context, list);
        this.settingList = list;
        this.context = context;
    }

    public void setIsFTR(boolean isFTR) {
        this.isFTR = isFTR;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        SettingHolder holder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_detail_lun, null, false);

            holder = new SettingHolder(cpt);
            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);

        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (SettingHolder) cpt.getTag();
        }
        holder.mi1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

            }
        });
        holder.mi2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

            }
        });
        holder.mt1.setText("发布时间" + settingList.get(i).getTime());
        holder.mt2.setText("用户" + settingList.get(i).getUser() + "评论:" +
                settingList.get(i).getText());

        if (settingList.get(i).getPinglun() != null) {
            holder.mt3.setText("发帖人回复" + settingList.get(i).getUser() + "：" +
                    settingList.get(i).getPinglun());
            holder.mt6.setText("(1)");
        } else {
            holder.mt3.setText("发帖人回复:暂无");
            holder.mt6.setText("(0)");
        }
        holder.mt5.setText("("+settingList.get(i).getDianzan()+")");
        holder.mi2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Integer dz = Integer.valueOf(settingList.get(i).getDianzan());
                dz=dz+1;
                settingList.get(i).setDianzan(dz);
                TbFourm js = settingList.get(i);

                ZZRHttp.postJson(UrlContant.URL + "updateFourm", new Gson().toJson(js), new ZZRCallBack.CallBackString() {
                    @Override
                    public void onFailure(int i, String s) {
                        new ToastDialog(getContext()).
                                setText("网络连接失败！")
                                .show();
                    }

                    @Override
                    public void onResponse(String s) {
                        db_User db_user1 = new Gson().fromJson(s, db_User.class);
                        if (db_user1.getCode().equals("200")){
                            new ToastDialog(context).
                                    setText("点赞成功")
                                    .show();
                        }else{
                            new ToastDialog(context).
                                    setText("网络连接失败！")
                                    .show();
                        }
                    }
                });
                notifyDataChanged();
            }
        });
        if (isFTR) {
            holder.mt8.setVisibility(Component.VISIBLE);
            holder.m10.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    String mt9 = holder.mt9.getText().toString().trim();
                    settingList.get(i).setPinglun(mt9);
                    String msl = new Gson().toJson(settingList.get(i));

                    ZZRHttp.postJson(UrlContant.URL + "updateFourm", msl, new ZZRCallBack.CallBackString() {
                        @Override
                        public void onFailure(int i, String s) {
                            new ToastDialog(getContext()).
                                    setText("网络连接失败！")
                                    .show();
                        }

                        @Override
                        public void onResponse(String s) {
                            db_User db_user1 = new Gson().fromJson(s, db_User.class);
                            if (db_user1.getCode().equals("200")){

                            }else{

                            }
                        }
                    });
                }
            });
        } else {
            holder.mt8.setVisibility(Component.HIDE);
        }
        return cpt;

    }

    private static class SettingHolder {
        DirectionalLayout mt8;
        Text m10;
        TextField mt9;
        Image mi1;
        Image mi2;
        Text mt1;
        Text mt2;
        Text mt3;
        Text mt5;
        Text mt6;

        SettingHolder(Component component) {
            mt1 = (Text) component.findComponentById(ResourceTable.Id_t1);
            mt2 = (Text) component.findComponentById(ResourceTable.Id_t2);
            mt3 = (Text) component.findComponentById(ResourceTable.Id_t3);
            mt5 = (Text) component.findComponentById(ResourceTable.Id_t5);
            mt6 = (Text) component.findComponentById(ResourceTable.Id_t6);
            m10 = (Text) component.findComponentById(ResourceTable.Id_t10);
            mt8 = (DirectionalLayout) component.findComponentById(ResourceTable.Id_t8);
            mt9 = (TextField) component.findComponentById(ResourceTable.Id_t9);
            mi1 = (Image) component.findComponentById(ResourceTable.Id_i1);
            mi2 = (Image) component.findComponentById(ResourceTable.Id_i2);
        }
    }
}
